﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;

public partial class Pages_User_CorrectPassword : System.Web.UI.Page
{
    /// <summary>
    /// 用户操作对象
    /// </summary>
    private UserManageDAO userDAO = new UserManageDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
    }

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitButton_Click(object sender, EventArgs e)
    {

        User user=(User)Session["login"];

        //更新密码
        if (this.IsPasswordTrue(user))
        {
            if (userDAO.UpdateUserByName(user))
            {
                Response.Write("<Script>alert('更改密码成功！！！')</Script>");
            }
            else
            {
                Response.Write("<Script>alert('更改密码失败！！！')</Script>");
            }
        }
        else
        {
            Response.Write("<Script>alert('旧密码不正确！！！')</Script>");
        }
    }

    /// <summary>
    /// 验证旧密码是否正确
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    private Boolean IsPasswordTrue(User user)
    {
        Boolean flag = false;
        if (null == user)
        {
            Response.Redirect(GlobalResource.LOGIN_PAGE);
        }
        else
        {
            if (this.passwordTextBox.Text.Trim().Equals(user.password))
            {
                user.password = this.newPasswordTextBox.Text.Trim();
                flag = true;
            }
        }
        return flag;
    }
   
    /// <summary>
    /// 将密码控件清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.passwordTextBox.Text = "";
        this.newPasswordTextBox.Text = "";
        this.reNewPasswordTextBox.Text = "";
    }
}